package com.example.bailin.giftsay.sort.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.entity.SingleEntity;
import com.example.bailin.giftsay.sort.LmClickListener;
import com.example.bailin.giftsay.tool.MyApp;

/**
 * Created by bailin on 16/8/19.
 *
 * 单品页右侧的ListView适配器
 * 不用优化
 */
public class SingleRightAdapter extends BaseAdapter {
    private SingleEntity entity;
    private LmClickListener lmClickListener;

    public void setLmClickListener(LmClickListener lmClickListener) {
        this.lmClickListener = lmClickListener;
    }

    public void setEntity(SingleEntity entity) {
        this.entity = entity;
    }

    @Override
    public int getCount() {
        return entity.getData().getCategories() == null ? 0 : entity.getData().getCategories().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(MyApp.getContext());
        SingleRightViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_listview_right, parent, false);
            viewHolder = new SingleRightViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (SingleRightViewHolder) convertView.getTag();
        }
        viewHolder.tvTitle.setText(entity.getData().getCategories().get(position).getName());
        SingleRightRvAdapter rvAdapter = new SingleRightRvAdapter();
        GridLayoutManager manager = new GridLayoutManager(MyApp.getContext(), 3);
        rvAdapter.setEntity(entity);
        rvAdapter.setIndex(position);
        rvAdapter.setLmClickListener(new LmClickListener() {
            @Override
            public void onClick(int id, String title) {
                lmClickListener.onClick(id, title);
            }
        });
        viewHolder.rv.setLayoutManager(manager);
        viewHolder.rv.setAdapter(rvAdapter);

        if (position == 0) {
            viewHolder.tvTitle.setVisibility(View.GONE);
            viewHolder.viewL.setVisibility(View.GONE);
            viewHolder.viewR.setVisibility(View.GONE);
        } else {
            viewHolder.tvTitle.setVisibility(View.VISIBLE);
            viewHolder.viewL.setVisibility(View.VISIBLE);
            viewHolder.viewR.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    private class SingleRightViewHolder {
        RecyclerView rv;
        TextView tvTitle;
        View viewL;
        View viewR;

        public SingleRightViewHolder(View v) {
            rv = (RecyclerView) v.findViewById(R.id.rv_right);
            tvTitle = (TextView) v.findViewById(R.id.tv_right_title);
            viewL = v.findViewById(R.id.view_left);
            viewR = v.findViewById(R.id.view_right);
        }
    }
}



















